package com.cloudbroker.bcs.common.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.cloudbroker.bcs.common.constants.BasicErrorCodes;
import com.cloudbroker.bcs.common.constants.BasicFields;
import com.cloudbroker.bcs.common.context.BizDataContext;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.exception.HSBCSErrInfoException;

public class VOUtil {
    
    public static Map<String, Object> genEmptyResult() {
        return new HashMap<String, Object>();
    }
    
    public static HSBCPSBackVO genEmptyBackVo() {
        return new HSBCPSBackVO();
    }
    
    public static Map<String, Object> setErrorResult(Map<String, Object> result, String errorNo) {
        result.put(BasicFields.ERROR_NO, errorNo);
        return result;
    }
    
    public static Map<String, Object> genJsonpSuccessResult(String callback) {
        Map<String, Object> result = genEmptyResult();
        result.put(BasicFields.JSONPCALLBACK, callback);
        return setSuccessResult(result);
    }
    
    public static Map<String, Object> setJsonpResult(Map<String, Object> result, String jsonpcallback) {
        if (StringUtils.isEmpty(ValueUtil.getString(result.get(BasicFields.ERROR_NO)))) {
            setSuccessResult(result);
        }
        result.put(BasicFields.JSONPCALLBACK, jsonpcallback);
        return result;
    }
    
    public static <T extends HSBCPSBackVO> T setJsonpResult(T result, String jsonpcallback) {
        result.setJsonpcallback(jsonpcallback);
        return result;
    }
    
    public static Map<String, Object> setErrorResult(Map<String, Object> result, String errorNo, String errorInfo) {
        result.put(BasicFields.ERROR_NO, errorNo);
        result.put(BasicFields.ERROR_INFO, errorInfo);
        return result;
    }
    
    public static Map<String, Object> genErrorResult(String errorNo) {
        Map<String, Object> result = genEmptyResult();
        result.put(BasicFields.ERROR_NO, errorNo);
        return result;
    }
    
    public static <T extends HSBCPSBackVO> T genErrorResult(String errorNo, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            t.setError_no(errorNo);
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
        
    }

    public static <T extends HSBCPSBackVO> T genErrorResult(String errorNo, String errorInfo, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            t.setError_no(errorNo);
            t.setError_info(errorInfo);
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 后台错误信息
     * */
    public static <T extends HSBCPSBackVO> T genBGErrorResult(String errorNo, String errorInfo, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            t.setError_no(errorNo);
            t.setError_info(errorInfo);
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static HSBCPSBackVO genErrorResultVo(String errorNo) {
        HSBCPSBackVO vo = new HSBCPSBackVO();
        vo.setError_no(errorNo);
        return vo;
    }
    
    public static HSBCPSBackVO genSuccessResultVo() {
        HSBCPSBackVO vo = new HSBCPSBackVO();
        vo.setError_no(BasicErrorCodes.SUCCESS);
        return vo;
    }
    
    public static Map<String, Object> genErrorResult(String errorNo, String errorInfo) {
        Map<String, Object> result = genEmptyResult();
        result.put(BasicFields.ERROR_NO, errorNo);
        result.put(BasicFields.ERROR_INFO, errorInfo);
        return result;
    }
    
    public static Map<String, Object> setSuccessResult(Map<String, Object> result, String errorInfo) {
        return setErrorResult(result, BasicErrorCodes.SUCCESS, errorInfo);
    }
    
    public static Map<String, Object> genSuccessResult() {
        Map<String, Object> result = genEmptyResult();
        return setSuccessResult(result);
    }
    
    public static HSBCPSBackVO genSuccessBackVo() {
        HSBCPSBackVO result = genEmptyBackVo();
        return setSuccessResult(result);
    }
    
    public static Map<String, Object> genSuccessResult(String errorInfo) {
        return genErrorResult(BasicErrorCodes.SUCCESS, errorInfo);
    }
    
    public static Map<String, Object> setSuccessResult(Map<String, Object> result) {
        result.put(BasicFields.ERROR_NO, BasicErrorCodes.SUCCESS);
        return result;
    }

    /**
     * 后台设置错误接口
     * */
    public static Map<String, Object> setBGErrorResult(Map<String, Object> result, String error_no, String error_info){
        result.put(BasicFields.ERROR_NO, error_no);
        result.put(BasicFields.ERROR_INFO, error_info);
        return result;
    }
    
    public static <T extends HSBCPSBackVO> T setSuccessResult(T result) {
        result.setError_no(BasicErrorCodes.SUCCESS);
        return result;
    }
    
    public static Map<String, Object> genCommonErrorResult() {
        return genErrorResult(BasicErrorCodes.COMMON_ERROR);
    }
    
    public static Map<String, Object> genCommonErrorResult(String errorInfo) {
        return genErrorResult(BasicErrorCodes.COMMON_ERROR, errorInfo);
    }
    
    public static Map<String, Object> genServiceErrorResult() {
        return genServiceErrorResult(null, null);
    }
    
    public static Map<String, Object> genServiceErrorResult(String errorOn) {
        return genServiceErrorResult(null, errorOn);
    }
    
    public static Map<String, Object> genServiceErrorResult(String errorInfo, String errorOn) {
        if (null == errorOn) {
            errorOn = BizDataContext.getBizServiceName();
        }
        return genErrorResult(BasicErrorCodes.SERVICE_ERROR, errorInfo, errorOn, null);
    }
    
    public static Map<String, Object> genErrorResult(String errorNo, String errorOn, Object errorValue) {
        return genErrorResult(errorNo, null, errorOn, errorValue);
    }
    
    public static Map<String, Object> genErrorResult(String errorNo, String errorInfo, String errorOn,
            Object errorValue) {
        Map<String, Object> result = genEmptyResult();
        setErrorResult(result, errorNo, errorInfo, errorOn, errorValue);
        return result;
    }
    
    public static Map<String, Object> setCommonErrorResult(Map<String, Object> result) {
        return setErrorResult(result, BasicErrorCodes.COMMON_ERROR);
    }
    
    public static Map<String, Object> setServiceErrorResult(Map<String, Object> result) {
        return setServiceErrorResult(result, null, null);
    }
    
    public static Map<String, Object> setServiceErrorResult(Map<String, Object> result, String errorOn) {
        return setServiceErrorResult(result, null, errorOn);
    }
    
    public static Map<String, Object> setServiceErrorResult(Map<String, Object> result, String errorInfo,
            String errorOn) {
        if (null == errorOn) {
            errorOn = BizDataContext.getBizServiceName();
        }
        return setErrorResult(result, BasicErrorCodes.SERVICE_ERROR, errorInfo, errorOn, null);
    }
    
    public static Map<String, Object> setErrorResult(Map<String, Object> result, String errorNo, String errorOn,
            Object errorValue) {
        return setErrorResult(result, errorNo, null, errorOn, errorValue);
    }
    
    public static Map<String, Object> setErrorResult(Map<String, Object> result, String errorNo, String errorInfo,
            String errorOn, Object errorValue) {
        result.put(BasicFields.ERROR_NO, errorNo);
        if (null != errorInfo) {
            result.put(BasicFields.ERROR_INFO, errorInfo);
        }
        if (null != errorOn) {
            result.put(BasicFields.ERROR_ON, errorOn);
        }
        if (null != errorValue) {
            result.put(BasicFields.ERROR_VALUE, errorValue);
        }
        return result;
    }
    
    public static Map<String, Object> genErrorResult(HSBCSErrInfoException exception) {
        return genErrorResult(exception.getErrorNo(), exception.getErrorInfo(), exception.getErrorOn(),
                exception.getErrorValue());
    }
    
    public static Map<String, Object> setExtErrorResult(Map<String, Object> result, String extErrorNo,
            String extErrorInfo) {
        result.put(BasicFields.EXT_ERROR_NO, extErrorNo);
        result.put(BasicFields.EXT_ERROR_INFO, extErrorInfo);
        return result;
    }
    
    public static boolean isSuccess(Map<String, Object> result) {
        if (null != result) {
            String errorNo = ValueUtil.getString(result.get(BasicFields.ERROR_NO));
            return StringUtils.isEmpty(errorNo) || BasicErrorCodes.SUCCESS.equals(errorNo);
        }
        return false;
    }

    public static boolean isSuccess(HSBCPSBackVO backVo) {
        if (null != backVo) {
            String errorNo = ValueUtil.getString(backVo.getError_no());
            return errorNo.isEmpty() || BasicErrorCodes.SUCCESS.equals(errorNo);
        }
        return false;
    }
    
    public static boolean isErrorOf(Map<String, Object> result, String errorNo) {
        if (null != result && null != errorNo) {
            String errorNoExists = ValueUtil.getString(result.get(BasicFields.ERROR_NO));
            return errorNo.equals(errorNoExists);
        }
        return false;
    }
    
    public static boolean isErrorIn(Map<String, Object> result, String... errorNos) {
        if (null != result && null != errorNos && 0 < errorNos.length) {
            String existsErrorNo = ValueUtil.getString(result.get(BasicFields.ERROR_NO));
            for (String errorNo : errorNos) {
                if (null != errorNo && errorNo.equals(existsErrorNo)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isErrorIn(HSBCPSBackVO backVo, String... errorNos) {
        if (null != backVo && null != errorNos && 0 < errorNos.length) {
            String existsErrorNo = ValueUtil.getString(backVo.getError_no());
            for (String errorNo : errorNos) {
                if (null != errorNo && errorNo.equals(existsErrorNo)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    public static Map<String, Object> setJsonpSuccessResult(Map<String, Object> result, String jsonpcallback) {
        setSuccessResult(result).put(BasicFields.JSONPCALLBACK, jsonpcallback);
        return result;
    }



}
